#include "udpserve.hpp"


void UsePage(char* argv[])
{
    std::cout << "failed Use" << std::endl;
    std::cout << argv[0] << "  [port]" << std::endl;
}

std::string Handler(const std::string &str)
{
    std::string res = "Server get a message: ";
    res += str;
    std::cout << res << std::endl;
    return res;
}

std::string Command(const std::string &str)
{
    FILE* pf = popen(str.c_str(), "r");
    char buffer[2048];
    std::string all;
    while(true)
    {
        char* tmp = fgets(buffer , sizeof(buffer) , pf);
        if(tmp == nullptr) break;

        all += buffer;
    }
    pclose(pf);
    return all;
}


int main(int argc , char* argv[])
{
    if(argc != 2)
    {
        UsePage(argv);
        exit(errno);
    }

    int port = std::stoi(argv[1]);
    UdpServe serve(port);
    serve.Init();

    serve.Run(Command);

    return 0;
}